package deal_data;
import java.io.*;


import config.Configuration;
import config.ConfigManager;
import data_structure.Node;
import data_structure.NodeList;

import op_database.DB_Operator;
import jxl.*;

/**
 * @author HaoChen
 * 此类定义了从xls文件读取文件简要处理,
 * 存入Cell_list链表数据结构并导入数据库
 * 
 */
public class DealData {
	
	private jxl.Workbook workbook;
	private NodeList cell_list=new NodeList();
	private String xls_file_address=null;
	private DB_Operator db_op;
	private Configuration config;
	public DealData(){
		config=ConfigManager.Instance().getConfig();
		xls_file_address=config.getXls_file_address();
		db_op=new DB_Operator();
		readData();
	}
	
	public void setFileName(String filename){
		xls_file_address=filename;
	}
	public void  readData()
	{
		try{
			InputStream is =new FileInputStream(xls_file_address);
			if(is==null)
				System.out.println("Sadf");
			workbook=Workbook.getWorkbook(is);
			Sheet st=workbook.getSheet(0);
			String time=st.getRow(1)[2].getContents();
			for(int i=1;i<st.getRows()-1;i++)
			{
				String next_time;
				Node tmp=new Node();
				if((next_time=st.getRow(i+1)[2].getContents()).equals(time))
				{
					time=next_time;
					continue;
				}
				else
				{
					tmp.setTime(time);
					time=next_time;

				}
				tmp.setName(st.getRow(i)[0].getContents());
				tmp.setDate(st.getRow(i)[1].getContents());
				tmp.setPrice(Double.parseDouble(st.getRow(i)[3].getContents()));
				tmp.setSize(Integer.parseInt(st.getRow(i)[4].getContents()));
				db_op.insertData(tmp);
				this.cell_list.add_cell(tmp);
				tmp.println();
			}
			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	public NodeList getCell_list() {
		return cell_list;
	}
	public void setCell_list(NodeList cell_list) {
		this.cell_list = cell_list;
	}
	public static void main(String[] args){
		DealData e=new DealData();
	}
}
